%author: wxj233
%time: 2023.10.25 12:00
%function: 主要用于完成特征点仿真以及轨迹模拟，整个场景在二维平面上进行

clc;
clear;
clear Function;
close all;

% 3倍sigma点就是精度
p_d = 0.4/3;  % 测距精度0.4m
p_v = 0.02778/3;  % 测速精度0.0277m/s
p_theta = 0.3/180*pi/3; % 测角精度0.3°（近距离），远距离是0.1°。都是波束中心附近

% 初始化仿真器
% dt
dt = 0.072;
simulater = Simulater(dt);

% 产生第一个目标特征点群
% len, width, probabilitys, varargin{seed}
cluster1_1 = simulater.generateFeaturePoints(10, 2.2, [0.5,0.5,0.5,0.5], 1);
cluster1_2 = simulater.generateFeaturePoints(10, 2.2, [0.5,0.5], 2);
% 根据特征点群，产生第一条轨迹
% cluster, point0, v, a, T0, Times, id, sdr, sdvr, sdtheta, varargin[seed1, seed2]
[T1_1_1, ~, ep1_1_1, ev1_1_1, et1_1_1] = simulater.generateTrack(cluster1_1, [-150, 105], [10, -10], [0, 1], 0, [0, 5;8,10], 1, p_d, p_v, p_theta, 11, 12);  % [[t, r, theta, vr, x, y, vx, vy, id]; ...]
[T1_2_1, ~, ep1_2_1, ev1_2_1, et1_2_1] = simulater.generateTrack(cluster1_2, [-150, 105], [10, -10], [0, 1], 0, [0, 10], 1, p_d, p_v, p_theta, 21, 22);  % [[t, r, theta, vr, x, y, vx, vy, id]; ...]

[T1_1_2, ~, ep1_1_2, ev1_1_2, et1_1_2] = simulater.generateTrack(cluster1_1, ep1_1_1, ev1_1_1, [0, 0], et1_1_1, [dt,4; 7,10], 1, p_d, p_v, p_theta, 31, 32);
[T1_2_2, ~, ep1_2_2, ev1_2_2, et1_2_2] = simulater.generateTrack(cluster1_2, ep1_2_1, ev1_2_1, [0, 0], et1_2_1, [dt, 10], 1, p_d, p_v, p_theta, 46, 47);

[T1_1_3, ~, ep1_1_3, ev1_1_3, et1_1_3] = simulater.generateTrack(cluster1_1, ep1_1_2, ev1_1_2, [0, -1], et1_1_2, [dt, 10], 1, p_d, p_v, p_theta, 36, 37);
[T1_2_3, ~, ep1_2_3, ev1_2_3, et1_2_3] = simulater.generateTrack(cluster1_2, ep1_2_2, ev1_2_2, [0, -1], et1_2_2, [dt*50, 10], 1, p_d, p_v, p_theta, 41, 42);


% 产生第二个目标特征点群
% len, width, probabilitys, varargin{seed}
cluster2_1 = simulater.generateFeaturePoints(5, 2.2, [0.5, 0.5, 0.5], 2);
% 根据特征点群，产生第二条轨迹
% cluster, point0, v, a, T0, Times, id, sdr, sdvr, sdtheta, varargin[seed1, seed2]
[T2_1, ~, ep2_1, ev2_1, et2_1] = simulater.generateTrack(cluster2_1, [-150, 0], [10, 10], [0, -1], 0, [0, 10], 2, p_d, p_v, p_theta, 51, 52);  % [[t, r, theta, vr, x, y, vx, vy, id]; ...]
[T2_2, ~, ep2_2, ev2_2, et2_2] = simulater.generateTrack(cluster2_1, ep2_1, ev2_1, [0, 0], et2_1, [dt, 10], 2, p_d, p_v, p_theta, 61, 62);
[T2_3, ~, ep2_3, ev2_3, et2_3] = simulater.generateTrack(cluster2_1, ep2_2, ev2_2, [0, 1], et2_2, [dt, 10], 2, p_d, p_v, p_theta, 61, 62);


% 产生第三个目标特征点群
% len, width, probabilitys, varargin{seed}
cluster3 = simulater.generateFeaturePoints(5, 2.2, [0.5, 0.5, 0.5,0.5], 3);
% % 根据特征点群，产生第三条轨迹
% cluster, point0, v, a, T0, Times, id, sdr, sdvr, sdtheta, varargin[seed1, seed2]
[T3_1, ~, ep3_1, ev3_1, et3_1] = simulater.generateTrack(cluster3, [-150, 60], [10, 0], [0, 0], 0, [0, 30], 3, p_d, p_v, p_theta, 71, 72);  % [[t, r, theta, vr, x, y, vx, vy, id]; ...]


% 引入噪声, 根据区域大小和噪声密度来算噪声数量，且数量服从泊松分布, 在区域内服从均匀分布，区域自己根据轨迹范围设定
% density varargin:{seed}
[noise, area] = simulater.generateNoise(0.001, 1); % [[t, r, theta, vr, x, y, vx, vy, id]; ...]

% vMax, dr, dvr, dtheta, q, p, vAssDoor, inDoor, initDoor, fplifeMax, tlifeMax, extR, amend
mfpTracer = MFPTracer(40, p_d^2, p_v^2, p_theta^2, 0.1, 0.5, 0.95, 0.8, 0.80, 60, 5, 15, 3);
[frame, isNextFrame]= simulater.getNextFrame();
frames = [];

count = 1;
while isNextFrame
    frame = mfpTracer.preproccess(frame, 10, 2, 1, 1, 10);  % 预处理points, epsilon, minpts, zoomX, zoomY, zoomV
    frames = [frames; frame];  % [1t, 2r, 3theta, 4vr, 5x, 6y, 7vx, 8vy, 9id, 10X, 11Y, 12cluster; ...]

%     figure(1);
%     gscatter(frames(:,10), frames(:,11), frames(:,12));hold on;  
% %     gscatter(frame(:,10), frame(:,11), frame(:,12));hold on; 
% %     disp("当前第："+num2str(simulater.timeIndex)+"帧");  % 还未被追踪
    disp("当前第："+num2str(count)+"帧");  % 还未被追踪
%     for track = mfpTracer.Tracks
%         for fp = track.fps
%             plot(fp.points(:,13), fp.points(:,15), 'DisplayName',"目标"+num2str(track.id)+"散射点轨迹"+num2str(fp.id));hold on;
%             te = "T"+num2str(fp.track.id)+" f:"+num2str(fp.id);
%             text(fp.points(end,13), fp.points(end,15), te);
%         end
%     end
%     hold off;
%     axis(area(1:4));
% %     title("轨迹信息"+num2str(simulater.timeIndex));
%     title("目标散射中心精细跟踪轨迹信息");
%     xlabel("x(m)");
%     ylabel("y(m)");
%     legend([]);
%     grid on;
%     if count == 146
%         disp("pause");
%     end
    
    mfpTracer.tracer(frame);
    [frame, isNextFrame]= simulater.getNextFrame();
    count = count+1;
end
mfpTracer.finish();
save("mfpTracer", 'mfpTracer');


figure(2)
gscatter(frames(:,10), frames(:,11), frames(:,9));hold on;
for track = mfpTracer.deadTracks  % [1t, 2x, 3vx, 4y,5vy; ...]
    if ~isempty(track.points) && size(track.points, 1) > 100
        plot(track.points(:,2), track.points(:,4),'DisplayName', "目标"+num2str(track.id));hold on;
    end
end
title("目标跟踪轨迹");
xlabel("x(m)");
ylabel("y(m)");
legend("","","","","目标1", "目标2", "目标3");
grid on;



